<template>
    <div class="modify-password">
      <div class="card">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>修改密码</span>

          </div>
          <div class="form">
            <el-form ref="form" :model="form" label-width="100px" :rules="rules">
              <el-form-item label="旧密码" prop="old_password">
                <el-input v-model="form.old_password"></el-input>
              </el-form-item>
              <el-form-item label="新密码" prop="new_password">
                <el-input v-model="form.new_password"></el-input>
              </el-form-item>
              <el-form-item label="重复新密码" prop="new_password1">
                <el-input v-model="form.new_password1"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="modifyPassword">更新密码</el-button>
                <el-button>取消</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </div>
    </div>
</template>

<script>
  export default {
    name: "ModifyPassword",
    data(){
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'))
        } else if (value !== this.form.new_password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      };
      return {
        form:{},
        rules:{
          old_password:[
            { required: true, message: '请输入旧密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          new_password:[
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          new_password1:[
            { required: true, validator: validatePass2,trigger: 'blur'},
          ],
        }
      }
    },
    methods:{
      modifyPassword(){
        this.$refs.form.validate(valid =>{
          if (valid){
            this.axios.post('/api/admin/user/modify-password',this.form).then(res=>{
              this.$message({
                type: 'success',
                message: '更新成功!'
              });
            }).catch(err=>{
              this.$message.error(err.response.data.message);
            });
          }
        });
      },
    }
  }
</script>

<style scoped lang="less">
.modify-password{
  width: 60%;
}
</style>
